package com.tuojiahr.action;

import java.io.IOException;

import com.opensymphony.xwork2.Preparable;
import com.tuojiahr.common.BaseActionCommon;
import com.tuojiahr.domain.TuoJiaHrSpecialColumnEntry;
import com.tuojiahr.domain.TuoJiaHrUser;
import com.tuojiahr.global.Constants;
import com.tuojiahr.pagemodel.PageModel;
import com.tuojiahr.service.SpecialEntryService;
import com.tuojiahr.tools.Pager;
import com.tuojiahr.tools.StringUtil;

/**
 * 说明：
 *
 * @author wang.haibo
 * @date 2012-11-10 下午11:19:04
 */

public class SpecialEntryAction extends BaseActionCommon implements Preparable {

	private static final long serialVersionUID = 1L;
	private SpecialEntryService specialEntryService; 
	
	private PageModel pageModel;
	private TuoJiaHrSpecialColumnEntry entry;
	private Pager pager;
	
	private String specialId;
	
	private String keyWord;
	private String url = "adminmgr/se_entryList";
	private String message = "出错啦！！！";
	
	private TuoJiaHrUser user = null;
	
	@Override
	public void prepare() throws Exception {
		user = getCurrUser();
	}
	
	/**
	 * 分页获取专栏下的条目，支持根据名称模糊查询
	 * @return
	 */
	public String entryList() {
		if(null != user) {
			if(StringUtil.isNotBlank(specialId)) {
				try {
					pageModel = specialEntryService.getEntryList(keyWord, Long.valueOf(specialId));
					return "entry_view";
				} catch (NumberFormatException e) {
					Constants.ExceptionLog.error("##SpecialEntryAction---entryList方法查询出错:", e);
				}
			}
		}
		return "msg";
	}
	
	/**
	 * 根据id查看一条记录
	 * @return
	 */
	public String openOneView() {
		if(null != user) { 
			String entryId = request.getParameter("entryId");
			if(StringUtil.isNotBlank(entryId)) {
				try {
					entry = specialEntryService.getEntryById(Long.valueOf(entryId));
					return "one_entry_view";
				} catch (Exception e) {
					Constants.ExceptionLog.error("##SpecialEntryAction---openOneView方法查询出错:", e);
				}
			}
		} 
		return "msg";
	}
	
	/**
	 * 打开添加或更新窗口
	 * @return
	 */
	public String openAddOrUpdate() {
		if(null != user) { 
			String entryId = request.getParameter("entryId");
			if(StringUtil.isNotBlank(entryId)) {
				entry = specialEntryService.getEntryById(Long.valueOf(entryId));
				url = "adminmgr/se_updateEntry";
			} else {
				url = "adminmgr/se_addEntry";
			}
			return "add_update_entry";
		}
		return "msg";
	}
	
	/**
	 * 添加
	 */
	public String addEntry() {
		if(null != user) { 
			entry.setOperator(user);
			try {
				specialEntryService.addEntry(entry, Long.valueOf(specialId));
				return "action";
			} catch (Exception e) {
				Constants.ExceptionLog.error("##SpecialEntryAction---addEntry方法添加出错:", e);
			}
		} 
		return "msg";
	}
	
	/**
	 * 编辑
	 */
	public String updateEntry() {
		if(null != user) { 
			if(null != entry && 0 != entry.getId()) {
				entry.setOperator(user);
				try {
					specialEntryService.updateEntry(entry,  Long.valueOf(specialId));
					return "action";
				} catch (NumberFormatException e) {
					Constants.ExceptionLog.error("##SpecialEntryAction---updateEntry方法更新出错:", e);
				}
			}
		}
		return "msg";
	}
	
	/**
	 * 删除，支持批量
	 * @throws IOException 
	 */
	public void delEntryById() throws IOException {
		String result = "fault";
		String ids = request.getParameter("ids");
		if(StringUtil.isNotBlank(ids)) {
			try {
				specialEntryService.deleteEntryById(ids);
				result = "success";
			} catch (Exception e) {
				result = "error";
				Constants.ExceptionLog.error("##SpecialEntryAction---delEntryById方法删除出错:", e);
			}
		}
		response.getWriter().print(result);
		response.getWriter().close();
	}
	
	public PageModel getPageModel() {
		return pageModel;
	}
	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}
	public TuoJiaHrSpecialColumnEntry getEntry() {
		return entry;
	}
	public void setEntry(TuoJiaHrSpecialColumnEntry entry) {
		this.entry = entry;
	}
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	public String getKeyWord() {
		return keyWord;
	}
	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public void setSpecialEntryService(SpecialEntryService specialEntryService) {
		this.specialEntryService = specialEntryService;
	}

	public String getSpecialId() {
		return specialId;
	}

	public void setSpecialId(String specialId) {
		this.specialId = specialId;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	
}
